package com.avic.rong.wedgit;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class ForbidSlideViewPager extends ViewPager {
    private boolean enabled;
    private boolean swipeEnabled;

    public ForbidSlideViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = false;
        this.swipeEnabled = false;
    }

    // 触摸没有反应就可以了
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            if (swipeEnabled) {
                // 拦截父view  swipeback 的 onInterceptTouchEvent 事件 解决与swipeback 滑动冲突
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /***
     * 拦截父view侧滑退出
     * @param swipeEnabled
     */
    public void setSwipeEnabled(boolean swipeEnabled) {
        this.swipeEnabled = swipeEnabled;
    }

}
